#import "YGDropdownListView.h"
@interface YGDropdownListView () <UITableViewDelegate,UITableViewDataSource> {



    BOOL showList;//是否弹出下拉列表

    CGFloat tabheight;//table下拉列表的高度

    CGFloat frameHeight;//frame的高度
    


}




@end







 

@implementation YGDropdownListView


@synthesize tv;







-(id)initWithFrame:(CGRect)frame

{

    if (frame.size.height>200) {

       frameHeight = 200;

    }else{

       frameHeight = 200;

    }

   tabheight = frameHeight-26;
    
    

    frame.size.height = 26.0f;
    frame.size.width  = 134.0f;
    
    

    self=[super initWithFrame:frame];



    if(self){

       showList = NO; //默认不显示下拉框

       

       tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 26, frame.size.width, frame.size.height)];

       tv.delegate = self;

       tv.dataSource = self;

      tv.backgroundColor = [UIColor whiteColor];

      tv.separatorColor = UIColorFromRGB(0x999999);

       tv.hidden = YES;

       [self addSubview:tv];
        
//        UIView *view = [[UIView alloc] init];
//        view.frame = CGRectMake(203.5,588,7,4);
//        view.backgroundColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
//        [self addSubview:view];



        self.textField = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 26)];
        self.textField.layer.borderWidth = 0.5;
        self.textField.layer.cornerRadius=4;
        self.textField.userInteractionEnabled = YES;
        self.textField.textColor = UIColorFromRGBAlpha(0x999999, 0.3);
        self.textField.font=[UIFont systemFontOfSize:14];
//      textField.borderStyle=UITextBorderStyleRoundedRect;//设置文本框的边框风格
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dropdown)];
        [self.textField addGestureRecognizer:tap];
//      [textField addTarget:self action:@selector(dropdown) forControlEvents:UIControlEventAllTouchEvents];

       [self addSubview:self.textField];

       

    }

   return self;

}
//设置placeholder样式
- (void)setDefaultContent:(NSString *)content{
    self.textField.text = content;
    self.textField.font=[UIFont systemFontOfSize:14];
    self.textField.textColor=UIColorFromRGB(0x999999);
    
}

- (void)setTableArray:(NSArray *)tableArray{
    _tableArray = tableArray;
    [self.tv reloadData];
    [self tableView:self.tv didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}

-(void)dropdown{

//   [textField resignFirstResponder];

   if (showList || self.tableArray.count == 0) {//如果下拉框已显示，什么都不做
       if (self.SelectCallBack) {
           self.SelectCallBack(-100);
       }
       return;

   }else {//如果下拉框尚未显示，则进行显示

       if (self.callBack) {
           self.callBack(YES);
       }

       CGRect sf = self.frame;

       sf.size.height = frameHeight;
      

       

      //把dropdownList放到前面，防止下拉框被别的控件遮住

      [self.superview bringSubviewToFront:self];

       tv.hidden = NO;

       showList = YES;//显示下拉框


       CGRect frame = tv.frame;

       frame.size.height = 0;

       tv.frame = frame;

       frame.size.height = tabheight;

      [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];

      [UIView setAnimationCurve:UIViewAnimationCurveLinear];

       self.frame = sf;

       tv.frame = frame;

      [UIView commitAnimations];

    }

}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

   return [self.tableArray count];

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdentifier = @"Cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    //设置下拉列表的样式

    cell.textLabel.text = [self.tableArray objectAtIndex:[indexPath row]];

   cell.textLabel.font = [UIFont systemFontOfSize:14.0f];

    cell.textLabel.textAlignment=NSTextAlignmentCenter;

   cell.accessoryType = UITableViewCellAccessoryNone;

   cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    cell.backgroundColor = [UIColor whiteColor];
    

    

    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 30;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    self.textField.text = [self.tableArray objectAtIndex:[indexPath row]];
    self.textField.textColor = UIColorFromRGB(0x333333);   //选中状态的文字颜色
    showList = NO;

    tv.hidden = YES;

    if (self.callBack) {
        self.callBack(NO);
    }
    CGRect sf = self.frame;
   sf.size.height = 26;
    self.frame = sf;

   CGRect frame = tv.frame;
   frame.size.height = 0;
    tv.frame = frame;
    if (self.SelectCallBack) {
        self.SelectCallBack(indexPath.row);
    }
}

- (BOOL)isShow{
    return showList;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    UIView *view = [super hitTest:point withEvent:event];
    if (view == nil) {
        for (UIView *subView in self.subviews) {
            CGPoint convertPoint = [subView convertPoint:point fromView:self];
            if (CGRectContainsPoint(subView.bounds, convertPoint)) {
                if ([subView isKindOfClass:[UITableView class]] ) {
                    view = subView;
                }
            }
        }
    }
    return view;
}
@end
